#include <iostream>
#include <unistd.h>
#include <fcntl.h>

using namespace std;

void SetNoneBlock(int fd)
{
    //1.先将fd的属性取出来
    int fd_set = fcntl(fd, F_GETFL);
    if(fd_set < 0)
    {
        cerr << "fcntl" << endl;
        exit(-1);
    }

    //2.再将fd的属性重新设置
    fcntl(fd, F_SETFL, fd_set | O_NONBLOCK);
}

int main()
{   
    char buffer[1024] = {0};
    SetNoneBlock(0);
    while(1)
    {
        int n = read(0,buffer,sizeof(buffer));
        if(n < 0)
        {
            cout << "fd is not ready!" << endl;
            sleep(1);
        }
        else
        {
            cout << "content is " << buffer << endl;
        }    
    }

    return 0;
}